package grpc

import (
	"context"
	"fmt"
	common "jihe.common"
	"jihe.common/protos/user_server/proto"
	"strings"
	"user_server/internal/models/c_user"
	"user_server/internal/models/d_user"
	"user_server/internal/models/d_user_audit"
	"user_server/internal/models/user"
)

func (s *Server) UserRegiste(c context.Context, req *proto.UserDUser) (ret *proto.UserDUser, err error) {
	dUserTbl := d_user.NewModel(s.Pgsql.Get())
	ret, err = dUserTbl.Create(req)
	if err != nil {
		if strings.Contains(err.Error(), "d_users_openid_uindex") {
			ret, err = s.UserGetByOpenid(c, req)
			if ret.Status != 1 {
				//账号已被禁用
				err = fmt.Errorf("errorno=1")
				return
			}
			return
		}
		common.Loger.Error("UserRegiste err:%v", err)
	}
	return
}

func (s *Server) UserGetByOpenid(c context.Context, req *proto.UserDUser) (ret *proto.UserDUser, err error) {
	dUserTbl := d_user.NewModel(s.Pgsql.Get())
	return dUserTbl.GetByOpenid(req.Openid)
}

func (s *Server) UserUpdateInfo(c context.Context, req *proto.UserDUser) (ret *proto.UserDUser, err error) {
	dUserTbl := d_user.NewModel(s.Pgsql.Get())
	return dUserTbl.UpdateInfoById(req)
}

func (s *Server) UserUpdatePhone(c context.Context, req *proto.UserDUser) (ret *proto.UserDUser, err error) {
	dUserTbl := d_user.NewModel(s.Pgsql.Get())
	return dUserTbl.UpdatePhoneById(req)
}

func (s *Server) UserGetByUid(c context.Context, req *proto.UserDUser) (ret *proto.UserDUser, err error) {
	dUserTbl := d_user.NewModel(s.Pgsql.Get())
	return dUserTbl.GetByUid(req.Id)
}

func (s *Server) UserSubmitAudit(c context.Context, req *proto.UserDUserAudit) (ret *proto.UserNull, err error) {
	ret = new(proto.UserNull)
	db := s.Pgsql.Get()
	tx, err := db.Begin()
	if err != nil {
		return
	}
	dUserAuditTbl := d_user_audit.NewTx(tx)
	err = dUserAuditTbl.DeleteByUid(req.Uid)
	if err != nil {
		tx.Rollback()
		return
	}
	err = dUserAuditTbl.Create(req)
	if err != nil {
		tx.Rollback()
		return
	}
	err = tx.Commit()
	return
}

func (s *Server) UserGetAuditByUids(c context.Context, req *proto.UserGetAuditByUidsReq) (ret *proto.UserDUserAudits, err error) {
	ret = new(proto.UserDUserAudits)
	dUserAuditTbl := d_user_audit.NewModel(s.Pgsql.Get())
	ret.List, err = dUserAuditTbl.GetByUids(req.Uids)
	return
}

func (s *Server) UserAuditStatus(c context.Context, req *proto.UserDUserAudit) (ret *proto.UserNull, err error) {
	ret = new(proto.UserNull)
	dUserAuditTbl := d_user_audit.NewModel(s.Pgsql.Get())
	if int(req.Status) == 2 {
		_ = user.NewModel(s.Pgsql.Get()).Create(&proto.UserUser{
			Uid:      req.Uid,
			Role:     1,
			Discount: 100,
		})
	}
	err = dUserAuditTbl.UpdateStatusByUid(req.Uid, int(req.Status))
	return
}

func (s *Server) UserUpdateStatus(c context.Context, req *proto.UserDUser) (ret *proto.UserNull, err error) {
	ret = new(proto.UserNull)
	dUserTbl := d_user.NewModel(s.Pgsql.Get())
	err = dUserTbl.UpdateStatusById(req)
	return
}

func (s *Server) UserDUserList(c context.Context, req *proto.UserListReq) (ret *proto.UserListRes, err error) {
	dUserTbl := d_user.NewModel(s.Pgsql.Get())
	req.Page--
	ret, err = dUserTbl.GetList(req)
	if err != nil {
		return
	}
	ret.Page = req.Page + 1
	ret.PageSize = req.PageSize
	return
}

func (s *Server) UpdateUserSex(c context.Context, req *proto.UserDUser) (ret *proto.UserNull, err error) {
	ret = new(proto.UserNull)
	dUserTbl := d_user.NewModel(s.Pgsql.Get())
	err = dUserTbl.UpdateUserSex(req)
	if err != nil {
		return
	}
	return
}

func (s *Server) UpdateUserBodyData(c context.Context, req *proto.UserDUser) (ret *proto.UserNull, err error) {
	ret = new(proto.UserNull)
	dUserTbl := d_user.NewModel(s.Pgsql.Get())
	err = dUserTbl.UpdateUserBodyData(req)
	if err != nil {
		return
	}
	return
}

func (s *Server) UserDUserListByIds(c context.Context, req *proto.UserDUserListByIdsReq) (ret *proto.UserDUsers, err error) {
	ret = new(proto.UserDUsers)
	dUserTbl := d_user.NewModel(s.Pgsql.Get())
	ret.List, err = dUserTbl.GetByIds(req.Uids)
	return
}

func (s *Server) UserCUserList(c context.Context, req *proto.UserListReq) (ret *proto.UserCListRes, err error) {
	ret = new(proto.UserCListRes)
	cUserTbl := c_user.NewModel(s.Pgsql.Get())
	return cUserTbl.GetList(req)
}
